package cn.richx.pushmessage.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 角色
 *
 * @author zhou
 */
@Entity
@Table(name = "kf_role")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Role implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    /**
     * 角色编码
     */
    @NotNull
    @Size(max = 50)
    @Id
    @Column(name = "role_code", length = 50)
    private String roleCode;
    /**
     * 角色名称
     */
    @NotNull
    @Size(max = 50)
    @Column(name = "role_name", length = 50)
    private String roleName;

    @JsonIgnore
    @ManyToMany
    @JoinTable(
        name = "kf_role_permission",
        joinColumns = {@JoinColumn(name = "role_code", referencedColumnName = "role_code")},
        inverseJoinColumns = {@JoinColumn(name = "permission_code", referencedColumnName = "permission_code")})
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @BatchSize(size = 20)
    private Set<Permission> permissions = new HashSet<>();


    public String getRoleCode() {
        return roleCode;
    }

    public void setRoleCode(String roleCode) {
        this.roleCode = roleCode;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Role role = (Role) o;
        return Objects.equals(roleCode, role.roleCode);
    }

    @Override
    public int hashCode() {
        return Objects.hash(roleCode);
    }

    @Override
    public String toString() {
        return "Role{" +
            "roleCode='" + roleCode + '\'' +
            ", roleName='" + roleName + '\'' +
            '}';
    }

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }
}
